package dang.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import dang.dao.ProductDao;
import dang.entity.Product;
import dang.exception.DangException;

public class BookDaoImpl  implements ProductDao {
	private Session s;
	@Autowired
	public void setS(Session s) {
		this.s = s;
	}

	@Override
	public List<Product> findByCatId(Integer catId, int begin, int size,String panxu) throws DangException {
		String hql="select b from CategoryProduct cp ,Book b where cp.product_id=b.id  " +
		"and cp.cat_id=? order by ?";
		Query query=s.createQuery(hql).setParameter(0, catId).setParameter(1, panxu);
		//分页
		query.setFirstResult(begin).setMaxResults(size);
		List<Product> products=query.list();
		return products;
	}
	
	@Override
	public List<Product> findLatest(int num) throws DangException {
		String hql="select b from CategoryProduct cp ,Book b where cp.product_id=b.id  " +
		"and cp.cat_id=2 order by b.dang_price";
		Query query=s.createQuery(hql);
		//分页
		query.setFirstResult(0).setMaxResults(num);
		List<Product> products=query.list();
		return products;
	}

	@Override
	public Product findById(int id) throws DangException {
		String hql="from Book b where b.id=?";
		Product p=(Product) s.createQuery(hql).setParameter(0, id).uniqueResult();
		return p;
	}

}
